//------------------------------------------------------------------------
// Abstract: Define of class SQLiteDB
//
// Authoring: XiaoHanxiong
//
// Time: 2014.03.14
//
// Static test:
//
// Version history:
//		Ver_1.0	: 2014.03.14	XiaoHanxiong
//------------------------------------------------------------------------
#ifndef __Database_SQLiteDB_h_
#define __Database_SQLiteDB_h_

#include "Stdafx.h"
#include "DBAdministrator.h"
#include "DBStructure.h"


// Forward declaration of sqlit3 struct type which is define in sqlite3.c.
// This is used for hiding details of sqlite3. So if there is a need to get
// to know the details of sqlite3, please see the www.sqlite3.com.org.
struct sqlite3;
struct sqlite3_stmt;

// Type define Sqlite database handle
typedef sqlite3 SqliteHandle;
typedef sqlite3_stmt SqliteStatement;

namespace Database
{
	#define SQLite_CodeUnValid	-1
	#define SQLite_TextMaxSize	512
	typedef int (*ExeCallBack)(void*, int, char**, char**);


	// SQLite data type
	enum _DATABASE SQLiteDataType
	{
		ESD_NULL = 0,
		ESD_INTEGER,
		ESD_TEXT,
		ESD_FLOAT,
		ESD_BLOB,
		//ESD_UNDEFINE
	};

	
	// Declear Sqlite database
	class _DATABASE SQLiteDB : public IDatabase
	{
	private:
		// Constructor
		SQLiteDB();
		// Destructor
		~SQLiteDB();
		// Administrate the create of DataBase object
		friend class DBAdministrator;

	public:
		// Open a database
		bool Open(const char* db, int flag);
		// Close a database
		void Close();
		// Execute SQL
		bool ExecuteSQL(const char* sql);
		// Prepare a SQL statement object
		bool PrepareSQL(const char* sql);
		// Close a SQL statement object
		void CloseSQL();
		// Step a SQL statement object
		bool Step();
		// Insert a record into database
		bool Insert();
		// Get last SQLite error information
		const char* GetLastError();
		// Set the callback function pointer
		ExeCallBack SetCallBackFunc(ExeCallBack callback);


	public:
		// Get the table column count
		bool GetColumnCount(int& count);
		// Get the table column name
		bool GetColumnName(int index, string& str);
		// Get the table column declare type
		bool GetColumnDeclareType(int index, string& str);
		// Get the table column storage type in SQLite
		bool GetColumnType(int index, SQLiteDataType& data_type);
		// Get field size
		int GetColumnSize(int index);

	public:
		// Begin/End an Transaction
		bool Transaction(bool flag);
		// Commit
		bool Commit();
		// Rollback
		bool Rollback();

	private:
		// Callback function
		static int CallBack(void* para, int column_count, char** p_value, char** p_name);

	private:
		// SQLite Database handle
		SqliteHandle* m_sqlite3_db;
		// SQL statement object
		SqliteStatement* m_sqlite_stmt;
		// Last return code
		int m_result_code;
		// Pointer to the call back function
		ExeCallBack m_callback;

	public:
		// Set field value by index, begin from 0
		bool Set(int index, unsigned int value);
		bool Set(int index, unsigned __int64 value);
		bool Set(int index, double value);
		bool Set(int index, string& value);
		bool Set(int index, const char* text, int size);
		bool Set(int index, const void* value, int size);
		bool Set(int index); // set null
		// Get field value by index, begin from 0
		bool Get(int index, unsigned int& value);
		bool Get(int index, unsigned __int64& value);
		bool Get(int index, double& value);
		bool Get(int index, string& value);
		bool Get(int index, const char* text, int size);
		bool Get(int index, const void*& value, int& size);
	};

}// _Database_

#endif

